package com.scalar.chiptrack.utils;

import java.awt.Rectangle;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.RandomAccessFile;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import com.scalar.ScalarException;
import com.scalar.chiptrack.db.DataSource;

public class XPDLGenerator
{
	private final static String XMLNS = "http://www.wfmc.org/2002/XPDL1.0";
	private final static String XMLNS_XPDL = "http://www.wfmc.org/2002/XPDL1.0";
	private final static String XMLNS_XSI = "http://www.w3.org/2001/XMLSchema-instance";
	private final static String XSI_SCHEMA_LOCATION = "http://www.wfmc.org/2002/XPDL1.0 http://wfmc.org/standards/docs/TC-1025_schema_10_xpdl.xsd";
	private static int Xaxis = 30;
	private static int Yaxis = 30;
	
	public static void main(String[] args) throws ScalarException 
	{
		Connection con;
		PreparedStatement pstmt;
		ResultSet rs;
		String baseProductName="";
		int baseProductID ;
		
		try
        {
            con = DataSource.getConnection();
            pstmt = con.prepareStatement("select * from product where P_Parent_Product_ID is null");
            rs = pstmt.executeQuery();
            while ( rs.next() )
            {
            	baseProductID = rs.getInt("Product_ID");
				baseProductName = rs.getString("Part_Number");
				System.out.println("Part_Number "+  baseProductName + " ID "+ baseProductID );
				HashMap activitiesByProcess = identifyActivites( baseProductID , baseProductName );
				createXPDL( activitiesByProcess, baseProductName );
				Xaxis = 30;
				Yaxis = 30;
            }
        }
        catch (SQLException e)
        {
            e.printStackTrace();
        }
	}

    private static void createXPDL(HashMap activitiesByProcess, String productName) throws ScalarException
    {
		ArrayList activityCollection = new ArrayList();
		Document document = null;
		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
		DocumentBuilder builder;
		try
		{
			builder = dbf.newDocumentBuilder();
			document = builder.newDocument();
			Node node = document.createElement("Package");
			((Element)node).setAttribute("Id", productName);
			((Element)node).setAttribute("xmlns", XMLNS);
			((Element)node).setAttribute("xmlns:xpdl", XMLNS_XSI);
			((Element)node).setAttribute("xmlns:xsi", XMLNS_XSI);
			((Element)node).setAttribute("xsi:schemaLocation", XSI_SCHEMA_LOCATION);
			Node workflowProcesses = (node.getOwnerDocument()).createElement("Workflowprocesses");
		
			Node workflowProcess = (workflowProcesses.getOwnerDocument()).createElement("workflowprocess");
			((Element)workflowProcess).setAttribute("Id", productName+"_Work");
			
			ActivitiesToXML(workflowProcess, productName, activitiesByProcess );
			
			workflowProcesses.appendChild(workflowProcess);
			node.appendChild(workflowProcesses);
			document.appendChild(node);
			
			buildActivitiesAndTransitionCollection(document, activityCollection );
			HashMap activityIdProcessstepIDMap = createMap( activityCollection );
			updateParentProductIdInTraverseTable( activityIdProcessstepIDMap );		
			transitionToXML(workflowProcess, productName, activityIdProcessstepIDMap );

		}
		catch (ParserConfigurationException e)
		{
			e.printStackTrace();
		}
		File f = null;
		try
		{
			//Setup directories
			File baseDir = new File(".");
			File outDir = new File(baseDir, ".");
			outDir.mkdirs();
			f = new File( outDir , productName+".xpdl" );
			//f = new File( "d:/chiptrack/"+productName+".xpdl" );
			if( f.exists() )
			{
				f.delete();
			}
			ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
			RandomAccessFile raf = new RandomAccessFile(f, "rw");
			TransformerFactory tFactory = TransformerFactory.newInstance();
			Transformer transformer = tFactory.newTransformer();
			transformer.setOutputProperty("indent","yes");
			transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount","4");
			transformer.setOutputProperty("encoding","UTF-8");
		         
			DOMSource source = new DOMSource( document );
			StreamResult result = new StreamResult( baos );
			transformer.transform( source , result );
			raf.write( baos.toByteArray() );
			baos.close();
			raf.close();
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
    }

    private static void updateParentProductIdInTraverseTable(HashMap activityIdProcessstepIDMap) throws ScalarException
    {
		Connection con = null;
		PreparedStatement pstmt;
		ResultSet rs;
		
		Iterator it = activityIdProcessstepIDMap.keySet().iterator();
		int i = 1;
		while ( it.hasNext() )
		{
			String fromProcessStepID = (String)it.next();
			try
			{
				con = DataSource.getConnection();
				pstmt = con.prepareStatement("update process_Step_Traverse_Table set Parent_Product_ID = (select P_Parent_Product_ID from product where processStep_ID = ?) where processStep_ID = ?");
				pstmt.setInt(1,Integer.parseInt(fromProcessStepID));
				pstmt.setInt(2,Integer.parseInt(fromProcessStepID));
				pstmt.executeUpdate();
			}
			catch (SQLException e)
			{
				e.printStackTrace();
			}
			finally
			{
				try{con.commit();
					con.close();}
				catch (SQLException e1){ e1.printStackTrace(); }
			}
		}        
    }

    private static void transitionToXML(Node workflowProcess, String productName, HashMap activityIdProcessstepIDMap) throws ScalarException
    {
		Node transitions = ( workflowProcess.getOwnerDocument()).createElement("Transitions");
		Connection con = null;
		PreparedStatement pstmt;
		ResultSet rs;
		
		Iterator it = activityIdProcessstepIDMap.keySet().iterator();
		int i = 1;
		while ( it.hasNext() )
		{
			String fromProcessStepID = (String)it.next();
			String toProcessStepID = "";
			try
			{
				con = DataSource.getConnection();
				pstmt = con.prepareStatement("select Next_ProcessStep_ID from process_Step_Traverse_Table where processStep_ID = ?");
				pstmt.setInt( 1 , Integer.parseInt(fromProcessStepID) );
				rs = pstmt.executeQuery();
				while ( rs.next() )
				{
					toProcessStepID = rs.getString("Next_ProcessStep_ID");
					String from = (String)activityIdProcessstepIDMap.get( fromProcessStepID );
					if( activityIdProcessstepIDMap.containsKey( toProcessStepID ) ){
						String to = (String)activityIdProcessstepIDMap.get( toProcessStepID );
						String edgeId = productName+"_Tran_"+(i++);
						
						Node transition = (transitions.getOwnerDocument()).createElement("Transition");
						((Element)transition).setAttribute("Id", edgeId);
						((Element)transition).setAttribute("From", from);
						((Element)transition).setAttribute("To", to);
						((Element)transition).setAttribute("Name", "");
	
						Node extendedAttributes = (transition.getOwnerDocument()).createElement("ExtendedAttributes");
				
						Node extendedAttribute =(extendedAttributes.getOwnerDocument()).createElement("ExtendedAttribute"); 
							((Element)extendedAttribute).setAttribute("Name", "bounds");
							((Element)extendedAttribute).setAttribute("Value", ("X:"+10+";"+"Y:"+10+";"+"W:"+20+";"+"H:"+20));
						extendedAttributes.appendChild(extendedAttribute);
				
						extendedAttribute =(extendedAttributes.getOwnerDocument()).createElement("ExtendedAttribute"); 
							((Element)extendedAttribute).setAttribute("Name", "transitionStepId");
							((Element)extendedAttribute).setAttribute("Value", "");
						extendedAttributes.appendChild(extendedAttribute);
				
						transition.appendChild(extendedAttributes);
						transitions.appendChild(transition);
					}
				}
				rs.close();
				pstmt.close();
			}
			catch (SQLException e)
			{
				e.printStackTrace();
			}
			finally
			{
				try{con.close();}
				catch (SQLException e1){ e1.printStackTrace(); }
			}
		}

		workflowProcess.appendChild(transitions);
    }

    private static void ActivitiesToXML(Node workflowProcess, String productName, HashMap activitiesByProcess)
    {
		Node activities = (workflowProcess.getOwnerDocument()).createElement("Activities");
		String[] processArray = new String[]{"FAB","Wafer Store","SORT","Die Bank","Assembly","TEST","Class Store","Mark & Pack","Finished Goods"};
		int x = 1;
		
		for ( int ind = 0 ; ind < processArray.length ; ind++ )
		{
			if( activitiesByProcess.containsKey( processArray[ind] ) )
			{
				ArrayList processList = (ArrayList) activitiesByProcess.get(processArray[ind]);
				for (int i=0 ; i < processList.size() ; i++)
				{
					ActivityToXML( activities , productName, (HashMap)processList.get(i), x++);
				}
			}
			Yaxis = 30;
			Xaxis = Xaxis + 100;
		}
		workflowProcess.appendChild(activities);
    }
    
	private static void ActivityToXML(Node activities, String productName, HashMap activityMap, int i )
	{
		Node activity = (activities.getOwnerDocument()).createElement("Activity");
			
		String attrId =  productName+"_Act_"+(i);
		
		HashMap product = (HashMap)activityMap.get("product") ;
		HashMap processStep = (HashMap)activityMap.get("processStep") ;
		String dupPartNumbers = (String)activityMap.get("dupPartNumbers");
		String vendorLocationDetails = (String)activityMap.get("vendorLocationDetails");
		String processParameters = (String)activityMap.get("processParameters");
		String processStepId = null;
		String processType = (String)processStep.get("PS_Type_Name");

		processStepId = (String)product.get("ProcessStep_ID");


		((Element)activity).setAttribute("Id", attrId);
		((Element)activity).setAttribute("name", (String)processStep.get("PS_Type") );
		((Element)activity).setAttribute("Type", (String)processStep.get("PS_Type_Name") );

		Node extendedAttributes = (activity.getOwnerDocument()).createElement("ExtendedAttributes");

		Node extendedAttribute =(extendedAttributes.getOwnerDocument()).createElement("ExtendedAttribute"); 
			((Element)extendedAttribute).setAttribute("Name", "bounds");
			((Element)extendedAttribute).setAttribute("Value", ("X:"+ Xaxis +";"+"Y:"+Yaxis +";"+"W:"+70+";"+"H:"+30));
			Yaxis = Yaxis + 80;
		extendedAttributes.appendChild(extendedAttribute);

		extendedAttribute = (extendedAttributes.getOwnerDocument()).createElement("ExtendedAttribute"); 
			((Element)extendedAttribute).setAttribute("Name", "processStepId");
			((Element)extendedAttribute).setAttribute("Value", processStepId);
		extendedAttributes.appendChild(extendedAttribute);

		extendedAttribute =(extendedAttributes.getOwnerDocument()).createElement("ExtendedAttribute"); 
			((Element)extendedAttribute).setAttribute("Name", "partnumber");
			((Element)extendedAttribute).setAttribute("Value", (String)product.get("Part_Number") );
		extendedAttributes.appendChild(extendedAttribute);

		extendedAttribute =(extendedAttributes.getOwnerDocument()).createElement("ExtendedAttribute"); 
			((Element)extendedAttribute).setAttribute("Name", "parameters");
			((Element)extendedAttribute).setAttribute("Value", processParameters);
		extendedAttributes.appendChild(extendedAttribute);

		extendedAttribute =(extendedAttributes.getOwnerDocument()).createElement("ExtendedAttribute"); 
			((Element)extendedAttribute).setAttribute("Name", "dupPartNumbers");
			((Element)extendedAttribute).setAttribute("Value", dupPartNumbers);
		extendedAttributes.appendChild(extendedAttribute);

		extendedAttribute =(extendedAttributes.getOwnerDocument()).createElement("ExtendedAttribute"); 
			((Element)extendedAttribute).setAttribute("Name", "comments");
			((Element)extendedAttribute).setAttribute("Value", (String)processStep.get("PS_Comments") );
		extendedAttributes.appendChild(extendedAttribute);

		extendedAttribute =(extendedAttributes.getOwnerDocument()).createElement("ExtendedAttribute"); 
			((Element)extendedAttribute).setAttribute("Name", "vendors");
			((Element)extendedAttribute).setAttribute("Value", vendorLocationDetails );
		extendedAttributes.appendChild(extendedAttribute);

		if( processType.equalsIgnoreCase("Finished Goods") )
		{
			extendedAttribute =(extendedAttributes.getOwnerDocument()).createElement("ExtendedAttribute"); 
			((Element)extendedAttribute).setAttribute("Name", "mktPartNumber");
			((Element)extendedAttribute).setAttribute("Value", (String)product.get("P_Marketing_Part_Number"));
			extendedAttributes.appendChild(extendedAttribute);
		}
		if( processType.equalsIgnoreCase("Finished Goods") )
		{
			extendedAttribute =(extendedAttributes.getOwnerDocument()).createElement("ExtendedAttribute"); 
			((Element)extendedAttribute).setAttribute("Name", "isApprovedProduct");
			((Element)extendedAttribute).setAttribute("Value", (String)product.get("IS_Approved"));
			extendedAttributes.appendChild(extendedAttribute);

            extendedAttribute =(extendedAttributes.getOwnerDocument()).createElement("ExtendedAttribute");
			((Element)extendedAttribute).setAttribute("Name", "isPrimaryFGProduct");
			((Element)extendedAttribute).setAttribute("Value", (String)product.get("isPrimaryFGProduct"));
			extendedAttributes.appendChild(extendedAttribute);
        }
		if( processType.equalsIgnoreCase("TEST") || processType.equalsIgnoreCase("Sort") )
		{
			extendedAttribute =(extendedAttributes.getOwnerDocument()).createElement("ExtendedAttribute"); 
			((Element)extendedAttribute).setAttribute("Name", "binsRequired");
			((Element)extendedAttribute).setAttribute("Value", (String)activityMap.get("binsRequired"));
			extendedAttributes.appendChild(extendedAttribute);
		}
		if( processType.equalsIgnoreCase("TEST") || processType.equalsIgnoreCase("Sort") )
		{
			extendedAttribute =(extendedAttributes.getOwnerDocument()).createElement("ExtendedAttribute"); 
			((Element)extendedAttribute).setAttribute("Name", "goodBinNumber");
			((Element)extendedAttribute).setAttribute("Value", (String)activityMap.get("goodBinNumber"));
			extendedAttributes.appendChild(extendedAttribute);
		}
		if( processType.equalsIgnoreCase("TEST") || processType.equalsIgnoreCase("Sort") )
		{
			extendedAttribute =(extendedAttributes.getOwnerDocument()).createElement("ExtendedAttribute"); 
			((Element)extendedAttribute).setAttribute("Name", "rejectedBinNumber");
			((Element)extendedAttribute).setAttribute("Value", (String)activityMap.get("rejectedBinNumber"));
			extendedAttributes.appendChild(extendedAttribute);
		}

		activity.appendChild(extendedAttributes);
		activities.appendChild(activity);
	}
    

    private static HashMap identifyActivites(int baseProductID, String baseProductName) throws ScalarException
    {
		Connection con= null;
		PreparedStatement pstmt= null;
		ResultSet rs= null;
		HashMap activitiesByProcess = new HashMap();
		HashMap processMap = new HashMap();
		
		try
		{
			con = DataSource.getConnection();
			pstmt = con.prepareStatement("select * from product where P_Parent_Product_ID = ?");
			pstmt.setInt( 1 , baseProductID );
			
			rs = pstmt.executeQuery();
			while ( rs.next() )
			{
				int productID = 0;
				productID = rs.getInt("Product_ID");
				processMap = getPropertyMap( productID );
				if( processMap != null && processMap.size() > 0  )
				{
					HashMap map = (HashMap)processMap.get("processStep");
					String processType = (String)map.get("PS_Type_Name");
					
					if ( activitiesByProcess.containsKey(processType))
					{
						ArrayList list =  (ArrayList) activitiesByProcess.get(processType);
						list.add(processMap);
						activitiesByProcess.put( processType , list );
					}
					else
					{
						ArrayList list =  new ArrayList();
						list.add(processMap);
						activitiesByProcess.put( processType , list );
					}
				}
			}
			rs.close();
			pstmt.close();
		}
		catch (SQLException e)
		{
			e.printStackTrace();
		}
		finally
		{
			try{con.close();}
            catch (SQLException e1){ e1.printStackTrace(); }
		}
		return activitiesByProcess;
    }

    private static HashMap getPropertyMap(int productID) throws ScalarException
    {
		HashMap processMap = new HashMap();
		HashMap product = new HashMap();
		HashMap processStep = new HashMap();
		String dupPartNumbers = "";
		String vendorLocationDetails = "";
		String processParameters = "";
		
		Connection con= null;
		PreparedStatement pstmt= null;
		ResultSet rs= null;
		
		product = getProductValues( productID );
		processStep = getProcessStepValues( (String)product.get("ProcessStep_ID") );
		dupPartNumbers = getDupPartNumbers ( (String)product.get("ProcessStep_ID") );
		vendorLocationDetails = getVendorLocationDetails( (String)product.get("ProcessStep_ID"), (String)processStep.get("PS_Type_Name") ); 
		processParameters = getProcessParameters( (String)product.get("ProcessStep_ID"), (String)processStep.get("PS_Type_Name") );
		
		processMap.put("product", product );
		processMap.put("processStep", processStep );
		processMap.put("dupPartNumbers", dupPartNumbers );
		processMap.put("vendorLocationDetails", vendorLocationDetails );
		processMap.put("processParameters", processParameters );
		
		if( ((String)processStep.get("PS_Type_Name")).equalsIgnoreCase("SORT") || ((String)processStep.get("PS_Type_Name")).equalsIgnoreCase("TEST") )
		{
			if ( getBinInformation( (String)product.get("ProcessStep_ID"), "" ).equalsIgnoreCase("true"))
			{
				processMap.put("binsRequired", "true" );
				processMap.put("goodBinNumber", getBinInformation( (String)product.get("ProcessStep_ID"), "good" ) );
				processMap.put("rejectedBinNumber", getBinInformation( (String)product.get("ProcessStep_ID"), "reject" ) );
			}
			else
			{
				processMap.put("binsRequired", "false" );
			} 
		}
		return processMap;
    }

    private static HashMap getProductValues( int productID ) throws ScalarException
    {
    	HashMap product = new HashMap();
		Connection con= null;
		PreparedStatement pstmt= null;
		ResultSet rs= null;
				
		try
		{
			con = DataSource.getConnection();
			pstmt = con.prepareStatement("select * from product where Product_ID = ?");
			pstmt.setInt( 1 , productID );
			rs = pstmt.executeQuery();
			if ( rs.next() )
			{
				product.put("Part_Number",rs.getString("Part_Number"));
				product.put("ProcessStep_ID",rs.getString("ProcessStep_ID"));
				product.put("P_Parent_Product_ID",rs.getString("P_Parent_Product_ID"));
				product.put("P_Marketing_Part_Number",rs.getString("P_Marketing_Part_Number"));
				product.put("P_Comments",rs.getString("P_Comments"));
				if( rs.getString("IS_Approved").equals("0"))
					product.put("IS_Approved","true");
				else	
					product.put("IS_Approved","false");
			}
			rs.close();
			pstmt.close();
		}
		catch (SQLException e)
		{
			e.printStackTrace();
		}
		finally
		{
			try{con.close();}
			catch (SQLException e1){ e1.printStackTrace(); }
		}
		return product;
    }
    
	private static HashMap getProcessStepValues(String processStepID) throws ScalarException
	{
		HashMap processStep = new HashMap();
		Connection con= null;
		PreparedStatement pstmt= null;
		ResultSet rs= null;
				
		try
		{
			con = DataSource.getConnection();
			pstmt = con.prepareStatement("select * from Process_Step where ProcessStep_ID = ?");
			pstmt.setInt( 1 , Integer.parseInt( processStepID )  );
			rs = pstmt.executeQuery();
			if ( rs.next() )
			{
				processStep.put("ProcessStep_ID",rs.getString("ProcessStep_ID"));
				processStep.put("PS_Type",rs.getString("PS_Type").trim());
				processStep.put("PS_Type_Name",rs.getString("PS_Type_Name").trim());
				processStep.put("PS_Comments",rs.getString("PS_Comments"));
			}
			rs.close();
			pstmt.close();
		}
		catch (SQLException e)
		{
			e.printStackTrace();
		}
		finally
		{
			try{con.close();}
			catch (SQLException e1){ e1.printStackTrace(); }
		}
		return processStep;
	}
	
	private static String getDupPartNumbers(String processStepID) throws ScalarException
	{
		String dupPartNumbers = "";
		Connection con= null;
		PreparedStatement pstmt= null;
		ResultSet rs= null;
				
		try
		{
			con = DataSource.getConnection();
			pstmt = con.prepareStatement("select Part_Number from product_map_table where processStep_ID = ?");
			pstmt.setInt( 1 , Integer.parseInt( processStepID )  );
			rs = pstmt.executeQuery();
			while ( rs.next() )
			{
				dupPartNumbers = dupPartNumbers +  rs.getString("Part_Number")+";" ;
			}
			rs.close();
			pstmt.close();
		}
		catch (SQLException e)
		{
			e.printStackTrace();
		}
		finally
		{
			try{con.close();}
			catch (SQLException e1){ e1.printStackTrace(); }
		}
		return dupPartNumbers;
	}
	
	private static String getVendorLocationDetails(String processStepID, String processType) throws ScalarException
    {
    	String vendorLocations = "";
		Connection con= null;
		PreparedStatement pstmt= null;
		ResultSet rs= null;
				
		try
		{
			con = DataSource.getConnection();
			pstmt = con.prepareStatement("select L_Vendor_Name+'/'+City,PSLT_Priority,PSLT_Split,PSLT_Capacity,PSLT_Cost_Normal,PSLT_Cost_Express,PSLT_Cycle_Time_Normal,PSLT_Cycle_Time_Express,PSLT_Expected_yield from Process_Step_Location_Table,Location, Address where Location.Address_ID = Address.Address_id and Location.Location_ID = Process_Step_Location_Table.Location_ID and  processStep_Id = ?");
			pstmt.setInt( 1 , Integer.parseInt( processStepID )  );
			System.out.println(processStepID);
			rs = pstmt.executeQuery();
			vendorLocations = "";
			while ( rs.next() )
			{
				vendorLocations = vendorLocations + "true;";
				vendorLocations = vendorLocations + rs.getString( "PSLT_Priority" )+";";
				vendorLocations = vendorLocations + rs.getString( 1 );
				
				if( !(processType.equalsIgnoreCase("Wafer Store") || processType.equalsIgnoreCase("Class Store") || processType.equalsIgnoreCase("Die Bank") || processType.equalsIgnoreCase("Finished Goods")))
				{
					vendorLocations = vendorLocations + ";" + rs.getString( "PSLT_Split" )+";";
					vendorLocations = vendorLocations + rs.getString( "PSLT_Capacity" )+";";
					vendorLocations = vendorLocations + rs.getString( "PSLT_Cost_Normal" )+";";
					vendorLocations = vendorLocations + rs.getString( "PSLT_Cost_Express" )+";";
					vendorLocations = vendorLocations + rs.getString( "PSLT_Cycle_Time_Normal" )+";";
					vendorLocations = vendorLocations + rs.getString( "PSLT_Cycle_Time_Express" )+";";
					vendorLocations = vendorLocations + rs.getString( "PSLT_Expected_yield" );
				}
				vendorLocations = vendorLocations + "#";
			}
			rs.close();
			pstmt.close();
		}
		catch (SQLException e)
		{
			e.printStackTrace();
		}
		finally
		{
			try{con.close();}
			catch (SQLException e1){ e1.printStackTrace(); }
		}
		return vendorLocations;
	}

	private static String getProcessParameters(String processStepID , String processType) throws ScalarException
	{
		String processParamters = "";
		Connection con= null;
		PreparedStatement pstmt= null;
		ResultSet rs= null;

		try
		{
			con = DataSource.getConnection();
			
			if(processType.equalsIgnoreCase("FAB"))
			{
				pstmt = con.prepareStatement("select FT_Lot_Hold_Yield_Limit,FT_Gross_Die_Wafer,FT_Standard_Unit_Cost,FT_Lot_Scrap_Yield_Limit,FT_Target_Yield from FAB_Table where ProcessStep_ID = ?");
				pstmt.setInt( 1 , Integer.parseInt( processStepID )  );
				rs = pstmt.executeQuery();
				if( rs.next() )
				{
					processParamters =  processParamters + "Lot Hold Yeild Limit=%="+ rs.getString("FT_Lot_Hold_Yield_Limit")+";";
					processParamters =  processParamters + "Gross Die/Wafer=%="+ rs.getString("FT_Gross_Die_Wafer")+";";
					processParamters =  processParamters + "Standard Unit Cost=$="+ rs.getString("FT_Standard_Unit_Cost")+";";
					processParamters =  processParamters + "Lot Scarp yeild Limit=%="+ rs.getString("FT_Lot_Scrap_Yield_Limit")+";";
					processParamters =  processParamters + "Target Yeild=%="+ rs.getString("FT_Target_Yield")+";";
				}
			}
			else if (processType.equalsIgnoreCase("Assembly"))
			{
				pstmt = con.prepareStatement("select AT_Package_ID, AT_Standard_Unit_Cost, AT_Target_Yield, AT_Lot_Scrap_Yield_Limit from Assembly_Table where ProcessStep_ID = ? ");
				pstmt.setInt( 1 , Integer.parseInt( processStepID )  );
				rs = pstmt.executeQuery();
				if( rs.next() )
				{
					processParamters =  processParamters + "Package ID=ID="+ rs.getString("AT_Package_ID")+";";
					processParamters =  processParamters + "Standard Unit Cost=$="+ rs.getString("AT_Standard_Unit_Cost")+";";
					processParamters =  processParamters + "Target Yeild=%="+ rs.getString("AT_Target_Yield")+";";
					processParamters =  processParamters + "Lot Scarp yeild Limit=%="+ rs.getString("AT_Lot_Scrap_Yield_Limit")+";";
				}
			}
			else if (processType.equalsIgnoreCase("Mark & Pack"))
			{
				pstmt = con.prepareStatement("select MPT_Target_Yield, MPT_Standard_Unit_Cost, MPT_Lot_Scrap_Yield_Limit, MPT_Visual_System, MPT_Packing_Method from Mark_Pack_Table where ProcessStep_ID = ? ");
				pstmt.setInt( 1 , Integer.parseInt( processStepID )  );
				rs = pstmt.executeQuery();
				if( rs.next() )
				{
					processParamters =  processParamters + "Target Yeild=%="+ rs.getString("MPT_Target_Yield")+";";
					processParamters =  processParamters + "Standard Unit Cost=$="+ rs.getString("MPT_Standard_Unit_Cost")+";";
					processParamters =  processParamters + "Lot Scarp yeild Limit=%="+ rs.getString("MPT_Lot_Scrap_Yield_Limit")+";";
					processParamters =  processParamters + "Visual System=="+ rs.getString("MPT_Visual_System")+";";
					processParamters =  processParamters + "Packing Method=="+ rs.getString("MPT_Packing_Method")+";";
				}
			}
			else if (processType.equalsIgnoreCase("TEST"))
			{
				pstmt = con.prepareStatement("select FTT_Target_Yield, FTT_Lot_Hold_Yield_Limit, FTT_Lot_Scrap_Yield_Limit, FTT_Standard_Unit_Cost, FTT_Test_Jig, FTT_System from Final_Test_Table where ProcessStep_ID = ?");
				pstmt.setInt( 1 , Integer.parseInt( processStepID )  );
				rs = pstmt.executeQuery();
				if( rs.next() )
				{
					processParamters =  processParamters + "Target Yeild=%="+ rs.getString("FTT_Target_Yield")+";";
					processParamters =  processParamters + "Lot Hold Yeild Limit=%="+ rs.getString("FTT_Lot_Hold_Yield_Limit")+";";
					processParamters =  processParamters + "Lot Scarp yeild Limit=%="+ rs.getString("FTT_Lot_Scrap_Yield_Limit")+";";
					processParamters =  processParamters + "Standard Unit Cost=$="+ rs.getString("FTT_Standard_Unit_Cost")+";";
					processParamters =  processParamters + "Test Jig=="+ rs.getString("FTT_Test_Jig")+";";
					processParamters =  processParamters + "System=="+ rs.getString("FTT_System")+";";
				}
			}
			else if (processType.equalsIgnoreCase("SORT"))
			{
				pstmt = con.prepareStatement("select ST_Expected_Net_Die_Wafer, ST_Lot_Hold_Yield_Limit, ST_Lot_Scrap_Yield_Limit, ST_Standard_Unit_Cost, ProbeCard_Id, ST_System, ST_RMA_Criteria, ST_Scrap_Criteria from SORT_TABLE where ProcessStep_ID = ? ");
				pstmt.setInt( 1 , Integer.parseInt( processStepID )  );
				rs = pstmt.executeQuery();
				if( rs.next() )
				{
					processParamters =  processParamters + "Expected Net Die/Wafer=%="+ rs.getString("ST_Expected_Net_Die_Wafer")+";";
					processParamters =  processParamters + "Lot Hold Yeild Limit=%="+ rs.getString("ST_Lot_Hold_Yield_Limit")+";";
					processParamters =  processParamters + "Lot Scarp yeild Limit=%="+ rs.getString("ST_Lot_Scrap_Yield_Limit")+";";
					processParamters =  processParamters + "Standard Unit Cost=$="+ rs.getString("ST_Standard_Unit_Cost")+";";
					processParamters =  processParamters + "Probe Card No=="+ rs.getString("ProbeCard_Id")+";";
					processParamters =  processParamters + "System=="+ rs.getString("ST_System")+";";
					processParamters =  processParamters + "RMA Criteria=="+ rs.getString("ST_RMA_Criteria")+";";
					processParamters =  processParamters + "Scrap Criteria=="+ rs.getString("ST_Scrap_Criteria")+";";
				}
			}
			else if (  processType.equalsIgnoreCase("Finished Goods"))
			{
				pstmt = con.prepareStatement("select IST_Min_Level, IST_Max_Level, IST_Reorder_Level from Inventory_Step_Table where ProcessStep_ID = ?");
				pstmt.setInt( 1 , Integer.parseInt( processStepID )  );
				rs = pstmt.executeQuery();
				if( rs.next() )
				{
					processParamters =  processParamters + "Reorder Level=units="+ rs.getString("IST_Reorder_Level")+";";
					processParamters =  processParamters + "Min Level=units="+ rs.getString("IST_Min_Level")+";";
					processParamters =  processParamters + "Max Level=units="+ rs.getString("IST_Max_Level")+";";
				}
			}
			else if ( processType.equalsIgnoreCase("Class Store") || processType.equalsIgnoreCase("Die Bank") || processType.equalsIgnoreCase("Wafer Store") )
			{
				pstmt = con.prepareStatement("select IST_Min_Level, IST_Max_Level, IST_Max_Time from Inventory_Step_Table where ProcessStep_ID = ?");
				pstmt.setInt( 1 , Integer.parseInt( processStepID )  );
				rs = pstmt.executeQuery();
				if( rs.next() )
				{
					processParamters =  processParamters + "Min Level=units="+ rs.getString("IST_Min_Level")+";";
					processParamters =  processParamters + "Max Level=units="+ rs.getString("IST_Max_Level")+";";
					processParamters =  processParamters + "Max Time=Days="+ rs.getString("IST_Max_Time")+";";
				}
			}
		}
		catch (SQLException e)
		{
			e.printStackTrace();
		}
		finally
		{
			try{con.close();}
			catch (SQLException e1){ e1.printStackTrace(); }
		}
		 
		return processParamters;
	}
	
	private static String getBinInformation( String processStepID, String flag ) throws ScalarException
	{
        String value="";
		Connection con= null;
		PreparedStatement pstmt= null;
		ResultSet rs= null;
				
		try
		{
			con = DataSource.getConnection();
			if( flag == null || flag.length() < 1 )
			{
				pstmt = con.prepareStatement("select count(*) from Wip_Step_Bin_Table where processStep_Id = ? ");
				pstmt.setInt( 1 , Integer.parseInt( processStepID )  );
				rs = pstmt.executeQuery();
				if ( rs.next() )
				{
					int i = rs.getInt(1);
					if( i==0 )
						value ="false";
					else	
						value = "true";
				}
				else
				{
					value = "false";
				}
			}
			else if (flag.equals("good") )
			{
				pstmt = con.prepareStatement("select count(*) from Wip_Step_Bin_Table where processStep_Id = ? and wsbt_Bin_Type = 'Accept' ");
				pstmt.setInt( 1 , Integer.parseInt( processStepID )  );
				rs = pstmt.executeQuery();
				if ( rs.next() )
				{
					value = rs.getString(1);
				}
				else
				{
					value = "";
				}
			}
			else if (flag.equals("reject") )
			{
				pstmt = con.prepareStatement("select count(*) from Wip_Step_Bin_Table where processStep_Id = ? and wsbt_Bin_Type = 'N/A' ");
				pstmt.setInt( 1 , Integer.parseInt( processStepID )  );
				rs = pstmt.executeQuery();
				if ( rs.next() )
				{
					value = rs.getString(1);
				}
				else
				{
					value = "";
				}
			}
			rs.close();
			pstmt.close();
		}
		catch (SQLException e)
		{
			e.printStackTrace();
		}
		finally
		{
			try{con.close();}
			catch (SQLException e1){ e1.printStackTrace(); }
		}
        return value; 
	}


	public static void writeFile(String productName, ArrayList activityCollection, ArrayList transitionCollection, String filePath)
	{
		Document document = null;
		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
		DocumentBuilder builder;
		try
		{
			builder = dbf.newDocumentBuilder();
			document = builder.newDocument();
			Node node = document.createElement("Package");
			((Element)node).setAttribute("Id", productName);
			((Element)node).setAttribute("xmlns", XMLNS);
			((Element)node).setAttribute("xmlns:xpdl", XMLNS_XSI);
			((Element)node).setAttribute("xmlns:xsi", XMLNS_XSI);
			((Element)node).setAttribute("xsi:schemaLocation", XSI_SCHEMA_LOCATION);
			Node workflowProcesses = (node.getOwnerDocument()).createElement("Workflowprocesses");

			Node workflowProcess = (workflowProcesses.getOwnerDocument()).createElement("workflowprocess");
			((Element)workflowProcess).setAttribute("Id", productName+"_Work");
			cellmapToXML(workflowProcess, productName, activityCollection );
			edgemapToXML(workflowProcess, productName, transitionCollection );
			workflowProcesses.appendChild(workflowProcess);
			node.appendChild(workflowProcesses);
			document.appendChild(node);
		}
		catch (ParserConfigurationException e)
		{
			e.printStackTrace();
		}
		File f = null;
		try
		{
			f = new File( filePath );
			if( f.exists() )
			{
				f.delete();
			}
			ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
			RandomAccessFile raf = new RandomAccessFile(f, "rw");
			TransformerFactory tFactory = TransformerFactory.newInstance();
			Transformer transformer = tFactory.newTransformer();
			transformer.setOutputProperty("indent","yes");
			transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount","4");
			transformer.setOutputProperty("encoding","UTF-8");
         
			DOMSource source = new DOMSource( document );
			StreamResult result = new StreamResult( baos );
			transformer.transform( source , result );
			raf.write( baos.toByteArray() );
			baos.close();
			raf.close();
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
	}
    
	public static void cellmapToXML(Node parent, String name, ArrayList activityCollection )
	{
		Node activities = (parent.getOwnerDocument()).createElement("Activities");
	
		for(int i=0; i < activityCollection.size(); i++)
		{
			Map map = (Map)activityCollection.get(i);
			Node activity = (activities.getOwnerDocument()).createElement("Activity");
			
			String attrId =  name+"_Act_"+(i+1);
			String processStepId = null;
			String tempId = null;
			String type = "";
			
			if( map.containsKey("processStepId") )
				processStepId = (String)map.get("processStepId");
			
			if( processStepId == null)
			{
				map.put("processStepId", "");
			}
			
			if( map.containsKey("attrId") ) 
				tempId = (String)map.get("attrId");
			
			if( tempId == null)
			{
				map.put("attrId", attrId);
			}
			else
			{
				attrId = tempId;
			}
			
			((Element)activity).setAttribute("Id", attrId);
			
			Object tempValue = "";
			if( map.containsKey( "value" ) )
				tempValue = map.get("value");
			
			if(tempValue instanceof String)
			{
				((Element)activity).setAttribute("name", (String)tempValue);
			}
			
			if( map.containsKey("Type") )
				type = (String)map.get("Type");
				
			((Element)activity).setAttribute("Type", type);
			
			Node extendedAttributes = (activity.getOwnerDocument()).createElement("ExtendedAttributes");
			Node extendedAttribute =(extendedAttributes.getOwnerDocument()).createElement("ExtendedAttribute"); 
			
			if( map.containsKey("bounds") )
			{
				Rectangle r = (Rectangle)map.get("bounds");
				((Element)extendedAttribute).setAttribute("Name", "bounds");
				((Element)extendedAttribute).setAttribute("Value", ("X:"+r.x+";"+"Y:"+r.y+";"+"W:"+r.width+";"+"H:"+r.height));
			}
			else
			{
				((Element)extendedAttribute).setAttribute("Name", "bounds");
				((Element)extendedAttribute).setAttribute("Value", ("X:"+10+";"+"Y:"+10+";"+"W:"+70+";"+"H:"+30));
			}
			
			extendedAttributes.appendChild(extendedAttribute);
			
			extendedAttribute = (extendedAttributes.getOwnerDocument()).createElement("ExtendedAttribute"); 
			
			((Element)extendedAttribute).setAttribute("Name", "processStepId");
			((Element)extendedAttribute).setAttribute("Value", processStepId);
			
			extendedAttributes.appendChild(extendedAttribute);
			
			extendedAttribute =(extendedAttributes.getOwnerDocument()).createElement("ExtendedAttribute"); 
			
			((Element)extendedAttribute).setAttribute("Name", "partnumber");
			
			if( map.containsKey( "partnumber") )
				((Element)extendedAttribute).setAttribute("Value", (String)map.get("partnumber"));
			else	
				((Element)extendedAttribute).setAttribute("Value", "");
			
			extendedAttributes.appendChild(extendedAttribute);
			
			extendedAttribute =(extendedAttributes.getOwnerDocument()).createElement("ExtendedAttribute"); 
			
			((Element)extendedAttribute).setAttribute("Name", "parameters");
			if( map.containsKey( "parameters") )
				((Element)extendedAttribute).setAttribute("Value", (String)map.get("parameters"));
			else	
				((Element)extendedAttribute).setAttribute("Value", "");
			extendedAttributes.appendChild(extendedAttribute);

			extendedAttribute =(extendedAttributes.getOwnerDocument()).createElement("ExtendedAttribute"); 
			
			((Element)extendedAttribute).setAttribute("Name", "dupPartNumbers");
			if( map.containsKey("dupPartNumbers") )
				((Element)extendedAttribute).setAttribute("Value", (String)map.get("dupPartNumbers"));
			else
				((Element)extendedAttribute).setAttribute("Value", "");	
			
			extendedAttributes.appendChild(extendedAttribute);
			
			extendedAttribute =(extendedAttributes.getOwnerDocument()).createElement("ExtendedAttribute"); 
			
			((Element)extendedAttribute).setAttribute("Name", "comments");
			
			if( map.containsKey("comments") )
				((Element)extendedAttribute).setAttribute("Value", (String)map.get("comments"));
			else	
				((Element)extendedAttribute).setAttribute("Value", "");
			extendedAttributes.appendChild(extendedAttribute);
			
			extendedAttribute =(extendedAttributes.getOwnerDocument()).createElement("ExtendedAttribute"); 
			((Element)extendedAttribute).setAttribute("Name", "vendors");
			if( map.containsKey("vendors") )
				((Element)extendedAttribute).setAttribute("Value", (String)map.get("vendors"));
			else	
				((Element)extendedAttribute).setAttribute("Value", "");
			extendedAttributes.appendChild(extendedAttribute);

			if( map.containsKey("mktPartNumber") )
			{
				extendedAttribute =(extendedAttributes.getOwnerDocument()).createElement("ExtendedAttribute"); 
				((Element)extendedAttribute).setAttribute("Name", "mktPartNumber");
				((Element)extendedAttribute).setAttribute("Value", (String)map.get("mktPartNumber"));
				extendedAttributes.appendChild(extendedAttribute);
			}
			if( map.containsKey("isApprovedProduct") )
			{
				extendedAttribute =(extendedAttributes.getOwnerDocument()).createElement("ExtendedAttribute"); 
				((Element)extendedAttribute).setAttribute("Name", "isApprovedProduct");
				((Element)extendedAttribute).setAttribute("Value", (String)map.get("isApprovedProduct"));
				//System.out.println("IS APPROVED PRODUCT "+ (String)map.get("isApprovedProduct") );
				extendedAttributes.appendChild(extendedAttribute);
			}
            if( map.containsKey("isPrimaryFGProduct") )
			{
				extendedAttribute =(extendedAttributes.getOwnerDocument()).createElement("ExtendedAttribute");
				((Element)extendedAttribute).setAttribute("Name", "isPrimaryFGProduct");
				((Element)extendedAttribute).setAttribute("Value", (String)map.get("isPrimaryFGProduct"));
				//System.out.println("IS APPROVED PRODUCT "+ (String)map.get("isApprovedProduct") );
				extendedAttributes.appendChild(extendedAttribute);
			}
            if( map.containsKey("binsRequired") )
			{
				extendedAttribute =(extendedAttributes.getOwnerDocument()).createElement("ExtendedAttribute"); 
				((Element)extendedAttribute).setAttribute("Name", "binsRequired");
				((Element)extendedAttribute).setAttribute("Value", (String)map.get("binsRequired"));
				extendedAttributes.appendChild(extendedAttribute);
			}
			if( map.containsKey("goodBinNumber") )
			{
				extendedAttribute =(extendedAttributes.getOwnerDocument()).createElement("ExtendedAttribute"); 
				((Element)extendedAttribute).setAttribute("Name", "goodBinNumber");
				((Element)extendedAttribute).setAttribute("Value", (String)map.get("goodBinNumber"));
				extendedAttributes.appendChild(extendedAttribute);
			}
			if( map.containsKey("rejectedBinNumber") )
			{
				extendedAttribute =(extendedAttributes.getOwnerDocument()).createElement("ExtendedAttribute"); 
				((Element)extendedAttribute).setAttribute("Name", "rejectedBinNumber");
				((Element)extendedAttribute).setAttribute("Value", (String)map.get("rejectedBinNumber"));
				extendedAttributes.appendChild(extendedAttribute);
			}
			
			activity.appendChild(extendedAttributes);
			activities.appendChild(activity);
		}
		
		parent.appendChild(activities);
	}
	
	public static void edgemapToXML(Node parent, String name, ArrayList transitionCollection)
	{
		Node transitions = (parent.getOwnerDocument()).createElement("Transitions");
		
		for(int i=0; i < transitionCollection.size(); i++)
		{
			Map map = (HashMap)transitionCollection.get(i);
			String from = (String)map.get("From");
			String to = (String)map.get("To");
			String edgeId = name+"_Tran_"+(i+1);
			
			if(map.get("edgeId") == null)
			{
				map.put("edgeId", edgeId);
			}
			
			String transitionStepId = (String)map.get("transitionStepId");
			
			if(transitionStepId == null){
				map.put("transitionStepId", "");
			}
			
			String value = (String)map.get("value");
			if(value == null){
				value = "Transition";
			}
			
			Node transition = (transitions.getOwnerDocument()).createElement("Transition");
			((Element)transition).setAttribute("Id", edgeId);
			((Element)transition).setAttribute("From", from);
			((Element)transition).setAttribute("To", to);
			((Element)transition).setAttribute("Name", value);
			
			Node extendedAttributes = (transition.getOwnerDocument()).createElement("ExtendedAttributes");
			Node extendedAttribute =(extendedAttributes.getOwnerDocument()).createElement("ExtendedAttribute"); 
			
			Rectangle r = (Rectangle)map.get("bounds");
			((Element)extendedAttribute).setAttribute("Name", "bounds");
			((Element)extendedAttribute).setAttribute("Value", ("X:"+r.x+";"+"Y:"+r.y+";"+"W:"+r.width+";"+"H:"+r.height));
			
			extendedAttributes.appendChild(extendedAttribute);
			
			extendedAttribute =(extendedAttributes.getOwnerDocument()).createElement("ExtendedAttribute"); 
			((Element)extendedAttribute).setAttribute("Name", "transitionStepId");
			((Element)extendedAttribute).setAttribute("Value", transitionStepId);
			
			extendedAttributes.appendChild(extendedAttribute);
			transition.appendChild(extendedAttributes);
			transitions.appendChild(transition);
		}
		
		parent.appendChild(transitions);
	}


	public static void buildActivitiesAndTransitionCollection(Document parent, ArrayList activities /*, ArrayList transitionList*/){
		
	   NodeList nodeList = parent.getElementsByTagName("Activities");
			
	   for(int i =0; i<nodeList.getLength();i++)
	   {
		   Element element = (Element)nodeList.item(i);
		   NodeList activity = element.getElementsByTagName("Activity");
		   createActivityMaps(activity, activities);
	   }
   }
   
   
   public  static void createActivityMaps(NodeList activity, ArrayList list)
   {
	   Map map = null;
	   for(int i=0; i<activity.getLength(); i++)
	   {
		   Element element = (Element)activity.item(i);
		   map = new HashMap();
		   map.put("value", element.getAttribute("name"));
		   map.put("attrId", element.getAttribute("Id"));
		   map.put("Type", element.getAttribute("Type"));

		   NodeList extendedAttribute = element.getElementsByTagName("ExtendedAttribute");
			
		   for(int extAttr =0; extAttr<extendedAttribute.getLength(); extAttr++){
			   element = (Element)extendedAttribute.item(extAttr);
				
			   if(element.getAttribute("Name").equalsIgnoreCase("bounds")){
				   String value = element.getAttribute("Value");
				   map.put("bounds", getRectangle(value));
			   }
				
			   if(element.getAttribute("Name").equalsIgnoreCase("processStepId")){
				   map.put("processStepId", element.getAttribute("Value"));
			   }
				
			   if(element.getAttribute("Name").equalsIgnoreCase("partnumber")){
				   map.put("partnumber", element.getAttribute("Value"));
			   }
			   if(element.getAttribute("Name").equalsIgnoreCase("parameters")){
				   map.put("parameters", element.getAttribute("Value"));
			   }
			   if(element.getAttribute("Name").equalsIgnoreCase("dupPartNumbers"))
			   {
				   map.put("dupPartNumbers", element.getAttribute("Value"));
			   }
			   if(element.getAttribute("Name").equalsIgnoreCase("mktPartNumber"))
			   {
				   map.put("mktPartNumber", element.getAttribute("Value"));
			   }
			   if(element.getAttribute("Name").equalsIgnoreCase("comments"))
			   {
				   map.put("comments", element.getAttribute("Value"));
			   }
			   if(element.getAttribute("Name").equalsIgnoreCase("vendors"))
			   {
				   map.put("vendors", element.getAttribute("Value"));
			   }
			   if(element.getAttribute("Name").equalsIgnoreCase("isApprovedProduct"))
			   {
					map.put("isApprovedProduct", element.getAttribute("Value"));
			   }
               if(element.getAttribute("Name").equalsIgnoreCase("isPrimaryFGProduct"))
			   {
					map.put("isPrimaryFGProduct", element.getAttribute("Value"));
			   }
               if(element.getAttribute("Name").equalsIgnoreCase("goodBinNumber"))
			   {
					map.put("goodBinNumber", element.getAttribute("Value"));
			   }
			   if(element.getAttribute("Name").equalsIgnoreCase("rejectedBinNumber"))
			   {
					map.put("rejectedBinNumber", element.getAttribute("Value"));
			   }
			   if(element.getAttribute("Name").equalsIgnoreCase("binsRequired"))
			   {
					map.put("binsRequired", element.getAttribute("Value"));
			   }			   
		   }
		   list.add(map);
	   }
   }
	
   public static Rectangle getRectangle(String bounds){
		
	   int length = bounds.length();
	   int index = bounds.indexOf(":");
	   bounds = bounds.substring(index, length);
	   int semiIndex = bounds.indexOf(";");
	   int X = Integer.parseInt(bounds.substring(index, semiIndex));
	   bounds = bounds.substring(semiIndex+1, bounds.length());
	   length = bounds.length();
	   index = bounds.indexOf(":");
	   bounds = bounds.substring(index, length);
	   semiIndex = bounds.indexOf(";");
	   int Y = Integer.parseInt(bounds.substring(index, semiIndex));
	   bounds = bounds.substring(semiIndex+1, bounds.length());
	   length = bounds.length();
	   index = bounds.indexOf(":");
	   bounds = bounds.substring(index, length);
	   semiIndex = bounds.indexOf(";");
	   int W = Integer.parseInt(bounds.substring(index, semiIndex));
	   bounds = bounds.substring(semiIndex+1, bounds.length());
	   length = bounds.length();
	   index = bounds.indexOf(":");
	   bounds = bounds.substring(index+1, length);
	   int H = Integer.parseInt(bounds);
	   return new Rectangle(X, Y, W, H);
   } 
  
	private static HashMap createMap(ArrayList activityCollection)
	{
		HashMap idProcessStepmap = new HashMap();
		for( int i=0 ; i < activityCollection.size() ; i++  )
		{
			HashMap map = (HashMap)activityCollection.get(i);
			if( map.containsKey("processStepId") && map.get("processStepId")!= null )
			{
				if( map.containsKey("attrId") )
					idProcessStepmap.put( map.get("processStepId"), map.get("attrId") );
			}
		}
		return idProcessStepmap;
	}
}
